package br.com.fafica.sisape.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.com.fafica.sisape.conexao.ConnectionFactory;
import br.com.fafica.sisape.model.Tratamento;

public class DAOTratamento implements InterfaceDAOTratamento{
	
	
	public void createTratamento(Tratamento tratamento, Integer cod) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "insert into Tratamento "
				+ "(CodUsuario, DataInicioTratamento, DataTerminoTratamento, Hospital_Clinica, Medico, Instrucoes) "
				+ "values (?,?,?,?,?,?)";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setInt(1, cod);
			stmp.setString(2, tratamento.getDataInicio());
			stmp.setString(3, tratamento.getDataTermino());
			stmp.setString(4, tratamento.getHospitalClinica());
			stmp.setString(5, tratamento.getMedico());
			stmp.setString(6, tratamento.getInstrucoes());
			stmp.execute();
			stmp.close();
			System.out.println("Salvo com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}

	}

	
	public ArrayList<Tratamento> readInternacao(Integer cod) throws SQLException {
		Connection con = new ConnectionFactory().getConnection();
		String seleciona = " where CodUsuario="+Integer.toString(cod);
		String sql = "Select * from Tratamento "+seleciona;

		try {
			PreparedStatement stmt = con.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();

			ArrayList<Tratamento> lista = new ArrayList<Tratamento>();
			while (rs.next()) {
				Tratamento tratamento = new Tratamento();
				tratamento.setDataInicio(rs.getString("DataInicioTratamento"));
				tratamento.setDataTermino(rs.getString("DataTerminoTratamento"));
				tratamento.setHospitalClinica(rs.getString("Hospital_Clinica"));
				tratamento.setMedico(rs.getString("Medico"));
				tratamento.setInstrucoes(rs.getString("Instrucoes"));
				tratamento.setCod(rs.getInt("idTratamento"));
				lista.add(tratamento);
			}
			rs.close();
			stmt.close();
			return lista;
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}
		return null;
	}

	
	public void updateTratamento(Tratamento tratamento) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "update Tratamento set DataInicioTratamento=?"
				+ " ,DataTerminoTratamento=?"
				+ " ,Hospital_Clinica=?" + " ,Medico=?"
				+ " ,Instrucoes=?"
				+ "where idTratamento=?";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setString(1, tratamento.getDataInicio());
			stmp.setString(2, tratamento.getDataTermino());
			stmp.setString(3, tratamento.getHospitalClinica());
			stmp.setString(4, tratamento.getMedico());
			stmp.setString(5, tratamento.getInstrucoes());
			stmp.setInt(6, tratamento.getCod());
			stmp.execute();
			stmp.close();
			System.out.println("Salvo com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}
	}

	
	public void deleteTratamento(Tratamento tratamento) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "delete from Tratamento where idTratamento=?";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setInt(1, tratamento.getCod());
			stmp.execute();
			stmp.close();
			System.out.println("Deletado com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}

	}

}
